﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_Admin_Link : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitMenu();
        if (!IsPostBack)
        {
            OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
            var links = from link in dc.Links
                        join link_category in dc.Link_Categories
                        on link.Link_category equals link_category.Category_id
                        where link_category.Category_name.Equals("Right")
                        orderby link.Link_sequence ascending
                        select new
                        {
                            标识 = link.Link_id,
                            标题 = link.Link_name,
                            序号 = link.Link_sequence,
                            链接 = link.URL
                        };
            LinkGridView.DataSource = links;
            LinkGridView.DataBind();
            dc.Dispose();
        }
    }

    protected void InitMenu()
    {

        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        Menu1.Items.Clear();
        MenuItem MainPage = new MenuItem();
        MainPage.Enabled = true;
        MainPage.Selectable = true;
        MainPage.Selected = false;
        MainPage.Value = MainPage.ToolTip = MainPage.Text = "注销登陆";
        MainPage.NavigateUrl = "../Default.aspx";
        Menu1.Items.Add(MainPage);

        MenuItem navigation = new MenuItem();
        navigation.Value = navigation.ToolTip = navigation.Text = "页眉管理";
        navigation.NavigateUrl = "Admin_navigation.aspx";
        Menu1.Items.Add(navigation);

        MenuItem news = new MenuItem();
        news.Value = news.ToolTip = news.Text = "重要通知";
        Menu1.Items.Add(news);

        MenuItem catergory1 = new MenuItem();//定义子菜单
        catergory1.Text = "类别管理";
        catergory1.NavigateUrl = "Admin_DocumentCategory.aspx";
        Menu1.Items[2].ChildItems.Add(catergory1);//添加子菜单

        MenuItem aa1 = new MenuItem();
        aa1.Text = "重要通知管理";
        aa1.NavigateUrl = "Admin_Notice.aspx";
        Menu1.Items[2].ChildItems.Add(aa1);


        MenuItem document = new MenuItem();
        document.Value = document.ToolTip = document.Text = "校发文件";
        Menu1.Items.Add(document);

        MenuItem catergory2 = new MenuItem();//定义子菜单
        catergory2.Text = "类别管理";
        catergory2.NavigateUrl = "Admin_DocumentCategory.aspx";
        Menu1.Items[3].ChildItems.Add(catergory2);//添加子菜单

        MenuItem aa2 = new MenuItem();
        aa2.Text = "校发文件管理";
        aa2.NavigateUrl = "Admin_Document.aspx";
        Menu1.Items[3].ChildItems.Add(aa2);





        int a = 0;

        var Menus = from menu in dc.Menus
                    select new
                    {
                        MenuItem_id = menu.MenuItem_id,
                        MenuItem_Name = menu.MenuItem_Name
                    };

        foreach (var menu in Menus)
        {
            MenuItem menuitem = new MenuItem();
            menuitem.Enabled = true;
            menuitem.Selectable = true;
            menuitem.Selected = false;
            menuitem.ToolTip = menuitem.Text = menu.MenuItem_Name.Trim();
            menuitem.Value = menu.MenuItem_id.ToString();
            Menu1.Items.Add(menuitem);
            a++;
            MenuItem catergory = new MenuItem();//定义子菜单
            catergory.Text = "类别管理";
            catergory.NavigateUrl = "Admin_DocumentCategory.aspx";
            Menu1.Items[a + 3].ChildItems.Add(catergory);//添加子菜单

            MenuItem aa = new MenuItem();

            aa.Text = menu.MenuItem_Name.ToString();


            aa.Text = aa.Text.Trim() + "管理";
            Menu1.Items[a + 3].ChildItems.Add(aa);

        }
        Menu1.Items[4].ChildItems[0].NavigateUrl = "";
        Menu1.Items[4].ChildItems[1].NavigateUrl = "";
        Menu1.Items[5].ChildItems[1].NavigateUrl = "Admin_Orgnization.aspx";
        Menu1.Items[6].ChildItems[1].NavigateUrl = "Admin_Rule.aspx";
        Menu1.Items[7].ChildItems[1].NavigateUrl = "Admin_Meeting.aspx";
        Menu1.Items[8].ChildItems[1].NavigateUrl = "Admin_CollegeCondition.aspx";
        Menu1.Items[9].ChildItems[1].NavigateUrl = "Admin_Law.aspx";
        Menu1.Items[10].ChildItems[1].NavigateUrl = "Admin_Download.aspx";







        MenuItem link = new MenuItem();
        link.Value = link.ToolTip = link.Text = "链接管理";
        link.NavigateUrl = "Admin_Link.aspx";
        Menu1.Items.Add(link);

        MenuItem password = new MenuItem();
        password.Value = password.ToolTip = password.Text = "密码管理";
        password.NavigateUrl = "Admin_Password.aspx";
        Menu1.Items.Add(password);


        dc.Dispose();
    }
    
    protected void Motify_Click(object sender, EventArgs e)
    {
        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        var motify_links = from link in dc.Links
                           where link.Link_id.Equals(Convert.ToInt32(Motify_LinkID.Text.Trim()))
                           select link;
        try
        {
            Convert.ToInt32(Motify_LinkSequence.Text.Trim());
        }
        catch (Exception ex)
        {
            JavaScript.JSDialog.Alert(this, "链接序号应为整数！");
            return;
        }
        if (Motify_LinkName.Text.Trim().Length == 0)
        {
            JavaScript.JSDialog.Alert(this, "链接名不能为空！");
            return;
        }
        else if (Motify_URL.Text.Trim().Length == 0)
        {
            JavaScript.JSDialog.Alert(this, "链接地址不能为空！");
            return;
        }

        motify_links.ToArray().ElementAt(0).Link_name = Motify_LinkName.Text.Trim();
        motify_links.ToArray().ElementAt(0).Link_sequence = Convert.ToInt32(Motify_LinkSequence.Text.Trim());
        motify_links.ToArray().ElementAt(0).URL = Motify_URL.Text.Trim();
        dc.SubmitChanges();

        var links = from link in dc.Links
                    join link_category in dc.Link_Categories
                    on link.Link_category equals link_category.Category_id
                    where link_category.Category_name.Equals("Right")
                    orderby link.Link_sequence ascending
                    select new
                    {
                        标识 = link.Link_id,
                        标题 = link.Link_name,
                        序号 = link.Link_sequence,
                        链接 = link.URL
                    };
        LinkGridView.DataSource = links;
        LinkGridView.DataBind();
        dc.Dispose();

        Motify_LinkName.Text = "";
        Motify_LinkSequence.Text = "";
        Motify_URL.Text = "";

    }
    protected void New_Click(object sender, EventArgs e)
    {
        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        if (New_LinkName.Text.Trim().Length != 0)
        {

            if(New_URL.Text.Trim().Length != 0)
            {
                var category = from cate in dc.Link_Categories
                               where cate.Category_name.Equals("Right")
                               select cate;
                
                Link link = new Link();
                try
                {
                    link.Link_name = New_LinkName.Text.Trim();
                    link.URL = New_URL.Text.Trim();
                    link.Link_sequence = Convert.ToInt32(New_LinkSequence.Text.Trim());
                    link.Link_category = category.ToArray().ElementAt(0).Category_id;
                }
                catch(Exception ex)
                {
                }
                SQL.SQLHelper.Insert(link);
                dc.SubmitChanges();
            }
            else
            {
                JavaScript.JSDialog.Alert(this, "链接地址不能为空！");
            }
        }
        else
        {
            JavaScript.JSDialog.Alert(this, "链接名不能为空！");
        }
        var links = from link in dc.Links
                    join link_category in dc.Link_Categories
                    on link.Link_category equals link_category.Category_id
                    where link_category.Category_name.Equals("Right")
                    orderby link.Link_sequence ascending
                    select new
                    {
                        标识 = link.Link_id,
                        标题 = link.Link_name,
                        序号 = link.Link_sequence,
                        链接 = link.URL
                    };
        LinkGridView.DataSource = links;
        LinkGridView.DataBind();
        dc.Dispose();

        New_LinkName.Text = "";
        New_URL.Text = "";
        New_LinkSequence.Text = "";


    }
    protected void LinkGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();

        var delete_link = from link in dc.Links
                          where link.Link_id.Equals(Convert.ToInt32(((Label)LinkGridView.Rows[e.RowIndex].FindControl("News_id")).Text))
                          select link;

        try
        {
            dc.Links.DeleteAllOnSubmit(delete_link);
            dc.SubmitChanges();
        }
        catch (Exception ex)
        {
        }


        var links = from link in dc.Links
                    join link_category in dc.Link_Categories
                    on link.Link_category equals link_category.Category_id
                    where link_category.Category_name.Equals("Right")
                    orderby link.Link_sequence ascending
                    select new
                    {
                        标识 = link.Link_id,
                        标题 = link.Link_name,
                        序号 = link.Link_sequence,
                        链接 = link.URL
                    };
        LinkGridView.DataSource = links;
        LinkGridView.DataBind();
        dc.Dispose();
    }
    protected void LinkGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Motify_LinkID.Text = ((Label)LinkGridView.Rows[e.NewEditIndex].FindControl("News_id")).Text.Trim();
        Motify_LinkName.Text = ((Label)LinkGridView.Rows[e.NewEditIndex].FindControl("News_Title")).Text.Trim();
        Motify_LinkSequence.Text = ((Label)LinkGridView.Rows[e.NewEditIndex].FindControl("Sequence")).Text.Trim();
        Motify_URL.Text = ((Label)LinkGridView.Rows[e.NewEditIndex].FindControl("URL")).Text.Trim();
    }
}
